﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace 药品信息管理
{
    public partial class QXGL : Form
    {
        DataTable dt_qx = new DataTable();

        string[] qxArray;

        public QXGL()
        {
            InitializeComponent();
        }
        public void check()
        {

        }
        //选择权限后加载相应的功能显示
        private void comboBoxEx1_SelectedIndexChanged(object sender, EventArgs e)
        { 
            foreach (DevComponents.DotNetBar.Controls.CheckBoxX  c in　panel2.Controls)
            {
                c.Checked = false;

            }
            foreach (DevComponents.DotNetBar.Controls.CheckBoxX c in panel3.Controls)
            {
                c.Checked = false;

            }
            try
            {
                using (SqlConnection con = new SqlConnection(SQL.connectDB))
                {
                    con.Open();
                    SqlCommand thisCommand = con.CreateCommand();
                    thisCommand.CommandText = "SELECT QX FROM ZD_QX WHERE MC='"+comboBoxEx1.Text.ToString()+"'";
                    object countResult = thisCommand.ExecuteScalar();

                    qxArray = Regex.Split(countResult.ToString(), ",", RegexOptions.IgnoreCase);
                    for (int i = 0; i < qxArray.Length; i++)
                    {
                        foreach (Control c in panel2.Controls)
                        {
                            if (c is DevComponents.DotNetBar.Controls.CheckBoxX)
                            {
                                if (((DevComponents.DotNetBar.Controls.CheckBoxX)c).Name == qxArray[i].ToString())
                                {
                                    ((DevComponents.DotNetBar.Controls.CheckBoxX)c).Checked = true;
                                }
                            }
                        }  
                        foreach (Control c in panel3.Controls)
                        {
                            if (c is DevComponents.DotNetBar.Controls.CheckBoxX)
                            {
                                if (((DevComponents.DotNetBar.Controls.CheckBoxX)c).Name == qxArray[i].ToString())
                                {
                                    ((DevComponents.DotNetBar.Controls.CheckBoxX)c).Checked = true;
                                }
                            }
                        }  
                     
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void QXGL_Load(object sender, EventArgs e)
        {
            try
            {
                using (SqlConnection con = new SqlConnection(SQL.connectDB))
                {
                    SqlDataAdapter da = new SqlDataAdapter();//实例化sqldataadpte
                    SqlCommand cmd = new SqlCommand("SELECT * FROM ZD_QX", con);//显示权限
                    da.SelectCommand = cmd;//设置为已实例化SqlDataAdapter的查询命令
                    da.Fill(dt_qx);
                    for (int i = 0; i < dt_qx.Rows.Count; i++)
                    {
                        comboBoxEx1.Items.Add(dt_qx.Rows[i][1].ToString());
                    }
              
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void ypzdwh_Click(object sender, EventArgs e)
        {
            ypzdwh.Checked = !ypzdwh.Checked;

        }

        private void buttonX4_Click(object sender, EventArgs e)
        {
            jbgl.Checked = !jbgl.Checked;
        }

        private void buttonX5_Click(object sender, EventArgs e)
        {
            jxgl.Checked = !jxgl.Checked;
        }

        private void buttonX6_Click(object sender, EventArgs e)
        {
            kcyj.Checked = !kcyj.Checked;
        }

        private void buttonX7_Click(object sender, EventArgs e)
        {
            yxqcx.Checked = !yxqcx.Checked;
        }

        private void buttonX8_Click(object sender, EventArgs e)
        {
            ljtz.Checked = !ljtz.Checked;
        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            if (comboBoxEx1.Text != "")
            {
                StringBuilder sb = new StringBuilder();
                foreach (Control c in panel3.Controls)
                {
                    if (c is DevComponents.DotNetBar.Controls.CheckBoxX)
                    {
                        if (((DevComponents.DotNetBar.Controls.CheckBoxX)c).Checked == true)
                        {
                            sb.Append(",");
                            sb.Append(c.Name.ToString());
                        }
                    }
                }
                foreach (Control c in panel2.Controls)
                {
                    if (c is DevComponents.DotNetBar.Controls.CheckBoxX)
                    {
                        if (((DevComponents.DotNetBar.Controls.CheckBoxX)c).Checked == true)
                        {
                            sb.Append(",");
                            sb.Append(c.Name.ToString());
                        }
                    }
                }
                try
                {

                    SqlConnection con = new SqlConnection(SQL.connectDB);
                    con.Open();
                    SqlCommand thisCommand = con.CreateCommand();
                    thisCommand.CommandText = "UPDATE ZD_QX SET QX='" + sb.ToString() + "' WHERE MC='" + comboBoxEx1.Text.ToString() + "'";
                    thisCommand.ExecuteNonQuery();
                    MessageBox.Show("修改成功!", "提示");

                }
                catch (Exception ee)
                {
                    MessageBox.Show(ee.ToString());
                }
            }
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            if (comboBoxEx1.Text != "")
            {
                try
                {

                    SqlConnection con = new SqlConnection(SQL.connectDB);
                    con.Open();
                    SqlCommand thisCommand = con.CreateCommand();
                    thisCommand.CommandText = "DELETE ZD_QX WHERE MC='" + comboBoxEx1.Text.ToString() + "'";
                    thisCommand.ExecuteNonQuery();
                    MessageBox.Show("删除成功!", "提示");
                }
                catch (Exception ee)
                {
                    MessageBox.Show(ee.ToString());
                }
            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            if (comboBoxEx1.Text != "")
            {
                StringBuilder sb = new StringBuilder();
                foreach (Control c in panel3.Controls)
                {
                    if (c is DevComponents.DotNetBar.Controls.CheckBoxX)
                    {
                        if (((DevComponents.DotNetBar.Controls.CheckBoxX)c).Checked == true)
                        {
                            sb.Append(",");
                            sb.Append(c.Name.ToString());
                        }
                    }
                }
                foreach (Control c in panel2.Controls)
                {
                    if (c is DevComponents.DotNetBar.Controls.CheckBoxX)
                    {
                        if (((DevComponents.DotNetBar.Controls.CheckBoxX)c).Checked == true)
                        {
                            sb.Append(",");
                            sb.Append(c.Name.ToString());
                        }
                    }
                }
                try
                {

                    SqlConnection con = new SqlConnection(SQL.connectDB);
                    con.Open();
                    SqlCommand thisCommand = con.CreateCommand();
                    thisCommand.CommandText = "INSERT INTO ZD_QX VALUES('" + sb.ToString() + "','" + comboBoxEx1.Text.ToString() + "')";
                    thisCommand.ExecuteNonQuery();
                    MessageBox.Show("增加成功!", "提示");
                }
                catch (Exception ee)
                {
                    MessageBox.Show(ee.ToString());
                }
            }
        }

        private void buttonX9_Click(object sender, EventArgs e)
        {
            yprk.Checked = !yprk.Checked;

        }

        private void buttonX10_Click(object sender, EventArgs e)
        {
            ypck.Checked = !ypck.Checked;
        }

        private void buttonX11_Click(object sender, EventArgs e)
        {
            yppd.Checked = !yppd.Checked;
        }


    }
}